package com.example.presentshopping.ui.activity

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.bean.MyearningsBean
import com.example.presentshopping.config.bean.SubsidiaryBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.tool.GsonUtils
import kotlinx.android.synthetic.main.activity_subsidiary.*


class SubsidiaryActivity : BaseMvpActivity() {
    var subsidiary: SubsidiaryBean? = null
    var baseRecyclerAdapter: BaseRecyclerAdapter<SubsidiaryBean.DataBean.ListBean>? = null
    var page = 1


    private var sendMesPresenter: PublicPresenter? = null

    override val layoutId: Int
        get() = R.layout.activity_subsidiary

    override fun initView(savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(this, false, "")
        sendMesPresenter?.attachView(this)
        title_main_tv.leftBackImageTv.setOnClickListener { finish() }
        recycler_view.useDefaultLoadMore()
        recycler_view.loadMoreFinish(false, true)
        recycler_view?.setLoadMoreListener {
            page++
            OnsInit()
        }

        setAdapter()
        OnsInit()
    }

    fun setAdapter() {
        baseRecyclerAdapter = object : BaseRecyclerAdapter<SubsidiaryBean.DataBean.ListBean>(this,
            null,
            R.layout.iemt_subsidiary) {
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: SubsidiaryBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val create_time = holder?.getView<TextView>(R.id.create_time)
                val total_tv = holder?.getView<TextView>(R.id.total_tv)

                create_time?.text =data.create_time

               when(data.status){
                   1->{
                       total_tv?.setTextColor(Color.parseColor("#F11801"))
                       total_tv?.text ="+ "+data.total
                   }
                   2->{
                       total_tv?.setTextColor(Color.parseColor("#00CC99"))
                       total_tv?.text ="- "+data.total
                   }

               }
            }

        }

        recycler_view.adapter =  baseRecyclerAdapter
    }


    fun OnsInit() {
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getPointsChange,
            HttpRequest.getTeamGoodsList(1, page, 20))
    }


    override fun onDestroy() {
        sendMesPresenter?.detachView()
        super.onDestroy()
    }

    override fun showToast(msg: String?) {
        ToastUtil.showMsg(msg)
    }

    override fun onSuccess(data: String?) {
        subsidiary = GsonUtils.getGson().fromJson(data,SubsidiaryBean().javaClass)
        if (subsidiary?.data?.list?.size!! > 0) {
            recycler_view.loadMoreFinish(false, true)
            baseRecyclerAdapter?.setData(subsidiary?.data?.list)
        } else {
            recycler_view.loadMoreFinish(true, false)
        }

    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }
}